﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceOne.Common;
using CommerceOne.Domains.Authentication;
using CommerceOne.Models;
using CommerceOne.Widgets.HeroImage;
using CommerceOne.Widgets.LeftCategoryNav;

namespace CommerceOne.Controllers
{
    public class ControllerBase : Controller
    {
        public HttpContextBase HttpContextBase { get; set; }

        
        public void CommonInitialize(ViewModelBase viewModelBase, Parameters parameter) 
        {
            if (viewModelBase == null) return;
            viewModelBase.LeftCategoryViewModel = LeftCategoryNavController.Instance.GetViewModel(parameter.ObjectId);

            HeroImageController.Instance.HttpContextBase = HttpContext;
            viewModelBase.HeroImage = HeroImageController.Instance.GetViewModel(parameter.ObjectId);

            if (parameter.PageType != PageType.Cart)
                viewModelBase.CartLink ="/Cart/CartIndex?returnUrl=" + HttpContext.Request.Url.PathAndQuery;
            else
            {
                viewModelBase.CartLink = "/Cart/CartIndex?returnUrl=" + HttpContext.Request["returnUrl"];
            }

            var cart = Session["Cart"] as Cart;
            if (cart != null)
            {
                viewModelBase.Cart = cart;
                viewModelBase.ItemNumber = cart.ComputeTotalItem();
            }

        }

        protected CommerceOnePrincipal CurrentPrincipal
        {
            get
            {
                if (!Request.IsAuthenticated) return null;
                return (CommerceOnePrincipal)System.Web.HttpContext.Current.User;
            }
        }
    }
}